package com.battery.system.service.battery.impl;

import com.battery.common.core.text.Convert;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.PurchaseBO;
import com.battery.system.domain.Purchase;
import com.battery.system.domain.PurchaseItems;
import com.battery.system.domain.PurchaseStatus;
import com.battery.system.domain.VO.PurchaseVO;
import com.battery.system.domain.VO.PurchaseVV;
import com.battery.system.mapper.PurchaseItemsMapper;
import com.battery.system.mapper.PurchaseMapper;
import com.battery.system.mapper.PurchaseStatusMapper;
import com.battery.system.service.battery.IPurchaseService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购订单Service业务层处理
 *
 * @author battery
 * @date 2021-01-22
 */
@Service
public class PurchaseServiceImpl extends BaseServiceImpl<Purchase> implements IPurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseItemsMapper purchaseItemsMapper;


    @Autowired
    private PurchaseStatusMapper purchaseStatusMapper;

    /**
     * 查询采购订单
     *
     * @param id 采购订单ID
     * @return 采购订单
     */
    @Override
    public Purchase selectPurchaseById(String id) {
        return purchaseMapper.selectPurchaseById(id);
    }

    /**
     * 查询采购订单列表
     *
     * @param purchase 采购订单
     * @return 采购订单
     */
    @Override
    public List<Purchase> selectPurchaseList(Purchase purchase) {
        return purchaseMapper.selectPurchaseList(purchase);
    }

    /**
     * 新增采购订单
     *
     * @param purchase 采购订单
     * @return 结果
     */
    @Override
    public int insertPurchase(Purchase purchase) {
        return purchaseMapper.insertPurchase(purchase);
    }

    /**
     * 修改采购订单
     *
     * @param purchase 采购订单
     * @return 结果
     */
    @Override
    public int updatePurchase(Purchase purchase) {
        return purchaseMapper.updatePurchase(purchase);
    }

    /**
     * 删除采购订单对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePurchaseByIds(String ids) {
        return purchaseMapper.deletePurchaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除采购订单信息
     *
     * @param id 采购订单ID
     * @return 结果
     */
    @Override
    public int deletePurchaseById(String id) {
        return purchaseMapper.deletePurchaseById(id);
    }

    @Override
    public List<PurchaseVV> selectPurchaseVVList(String id, Long status, String order) {
        List<PurchaseVV> list = purchaseMapper.selectPurchaseVVList(id, status, order);
        if (list != null) {
            for (PurchaseVV vv : list) {
                PurchaseItems purchaseItems = new PurchaseItems();
                purchaseItems.setPurchaseId(vv.getId());
                List<PurchaseItems> list1 = purchaseItemsMapper.selectPurchaseItemsList(purchaseItems);
                vv.setPurchaseItemsList(list1);
            }
        }
        return list;
    }

    /**
     * 展示采购订单列表
     *
     * @param purchaseBO
     * @return
     */
    @Override
    public List<PurchaseVO> selectPurchaseVOList(PurchaseBO purchaseBO) {
        return purchaseMapper.selectPurchaseVOList(purchaseBO);
    }

    @Transactional
    @Override
    public int delivery(Purchase purchase) {
        purchaseMapper.updateByPrimaryKeySelective(purchase);
        PurchaseStatus purchaseStatusUpdate = new PurchaseStatus();
        purchaseStatusUpdate.setPurchaseId(purchase.getId());
        // 订单状态  1 待发货  2 已发货  0已取消
        purchaseStatusUpdate.setOrderStatus(2L);
        purchaseStatusUpdate.setDeliverTime(DateUtils.getNowDate());
        return purchaseStatusMapper.updateByPrimaryKeySelective(purchaseStatusUpdate);
    }
}
